package cn.deschen.app.controller;

import cn.deschen.app.vo.CreativeDetailVO;
import cn.deschen.biz.domain.entity.AdCreative;
import cn.deschen.biz.dto.creative.CreativeAddDTO;
import cn.deschen.biz.dto.creative.CreativeQueryDTO;
import cn.deschen.biz.dto.creative.CreativeUpdateDTO;
import cn.deschen.biz.service.AdCreativeService;
import cn.deschen.common.api.CmPage;
import cn.deschen.common.api.CmRes;
import cn.deschen.common.config.security.UserDetails;
import cn.deschen.common.config.security.UserInfoContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author hanbin_chen
 * @Description 创意广告控制层
 * @DateTime 2021/06/18
 * @Version V1.0.0
 */
@RestController
@Slf4j
@RequestMapping("/ad/creative")
@Api(tags = "创意广告管理")
public class AdCreativeController {

    @Autowired
    private AdCreativeService creativeService;

    @Autowired
    private MapperFacade mapperFacade;

    @ApiOperation("创建广告")
    @PostMapping()
    public CmRes<Void> add(@Valid @RequestBody CreativeAddDTO addDTO) {
        UserDetails currentUser = UserInfoContext.getCurrentUser();
        AdCreative creative = mapperFacade.map(addDTO, AdCreative.class);
        creative.setUserId(currentUser.getId());

        creativeService.save(creative);
        return CmRes.success();
    }

    @ApiOperation("更新广告")
    @PutMapping()
    public CmRes<Void> update(@Valid @RequestBody CreativeUpdateDTO updateDTO) {
        AdCreative creative = mapperFacade.map(updateDTO, AdCreative.class);
        creativeService.updateById(creative);
        return CmRes.success();
    }

    @ApiOperation("删除广告")
    @DeleteMapping()
    public CmRes<Void> delete(@RequestParam List<Long> creativeIdList) {
        creativeService.removeByIds(creativeIdList);
        return CmRes.success();
    }

    @ApiOperation("获取广告列表")
    @GetMapping("/page")
    public CmRes<CmPage<AdCreative>> page(CreativeQueryDTO queryDTO,
                                          @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                          @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        QueryWrapper<AdCreative> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(AdCreative::getUpdateTime);
        if (!StringUtils.isEmpty(queryDTO.getCreativeName())) {
            wrapper.lambda().like(AdCreative::getCreativeName, queryDTO.getCreativeName());
        }
        if (!StringUtils.isEmpty(queryDTO.getMaterialType())) {
            wrapper.lambda().eq(AdCreative::getMaterialType, queryDTO.getMaterialType());
        }
        if (!StringUtils.isEmpty(queryDTO.getChildMaterialType())) {
            wrapper.lambda().eq(AdCreative::getChildMaterialType, queryDTO.getChildMaterialType());
        }
        if (!StringUtils.isEmpty(queryDTO.getAuditStatus())) {
            wrapper.lambda().eq(AdCreative::getAuditStatus, queryDTO.getAuditStatus());
        }
        if (!StringUtils.isEmpty(queryDTO.getUrl())) {
            wrapper.lambda().like(AdCreative::getUrl, queryDTO.getUrl());
        }
        List<AdCreative> creativeList = creativeService.list(wrapper);
        return CmRes.success(CmPage.restPage(creativeList));
    }

    @ApiOperation("根据主键获取广告详情")
    @GetMapping("/detail")
    public CmRes<CreativeDetailVO> detail(@ApiParam("创意广告id") @RequestParam Long id) {

        AdCreative creative = creativeService.getById(id);
        CreativeDetailVO detailVO = mapperFacade.map(creative, CreativeDetailVO.class);
        return CmRes.success(detailVO);
    }
}


